﻿using Coldairarrow.Util;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Text;

namespace Coldairarrow.Api.Controllers.UploadFile
{
    [Route("/Base_Company/[controller]/[action]")]
    public class UploadFileController : BaseController
    {

        readonly IConfiguration _configuration;
        public UploadFileController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        #region 保存
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            path = $"/Upload/Image/Base_Company/{DateTime.Now.ToString("yyyyMMdd")}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveContentImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            path = $"/Upload/Image/Content/{DateTime.Now.ToString("yyyyMMdd")}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        #endregion

        #region 客服图片上传
        /// <summary>
        /// 后台——客服图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveCServiceImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            string pStr = Create(8);
                            path = $"/Upload/Image/ConsumeServer/{DateTime.Now.ToString("yyyyMMdd")}/{pStr}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        #endregion

        #region 模板图片保存
        /// <summary>
        /// 模板图片保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveTempImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            path = $"/Upload/Image/Template/{DateTime.Now.ToString("yyyyMMdd")}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 内容管理图片上传
        /// <summary>
        /// 内容管理图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveAdvertiseImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            string pStr = Create(8);
                            path = $"/Upload/Image/SliderImg/{DateTime.Now.ToString("yyyyMMdd")}/{pStr}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                        else
                        {

                            throw new BusException("文件格式不正确，请选择图片！");
                            //res = new { success = false, message = "文件格式不正确！", Data = new object() };
                            //return JsonContent(res.ToJson());
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 劳动争议调解文件上传
        /// <summary>
        /// 劳动争议调解文件上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveAdvertiseFile()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".doc" || _tp.ToLower() == ".docx" || _tp.ToLower() == ".xls" || _tp.ToLower() == ".xlsx" || _tp.ToLower() == ".pdf" || _tp.ToLower() == ".ppt" || _tp.ToLower() == ".pptx")
                    {
                        path = $"/Upload/Document/SXGH_Advertise/{DateTime.Now.ToString("yyyyMMdd")}/{_file[0].FileName.Replace(_tp, "") + DateTime.Now.ToString("yyyyMMddHHmmss") + _tp }";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    else
                    {
                        res = new { success = false, message = "文件格式不支持！", Data = new object() };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 律所图片上传
        /// <summary>
        /// 律所图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveOfficeImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            string pStr = Create(8);
                            path = $"/Upload/Image/SliderImg/{DateTime.Now.ToString("yyyyMMdd")}/{pStr}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                        else
                        {
                            res = new { success = false, message = "文件格式不支持！", Data = new object() };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 律师图片上传
        /// <summary>
        /// 律师图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveLawyerImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            string pStr = Create(8);
                            path = $"/Upload/Image/SliderImg/{DateTime.Now.ToString("yyyyMMdd")}/{pStr}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                        else
                        {
                            res = new { success = false, message = "文件格式不支持！", Data = new object() };
                        }
                    }
                     
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 申请处理结果图片上传
        /// <summary>
        /// 后台——保存劳动争议调解结果图片(仅支持Img(.jpg,.jpeg,.png)格式)
        /// </summary>
        /// <param name="input">申请Id</param>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveTJJGImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png")
                    {
                        var pstr = DateTime.Now.ToString("yyyyMMddHHmmss") +  Create(6);
                        path = $"/Upload/Image/Base_FromImg/{pstr}/{_file[0].FileName}";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    else
                    {
                        res = new { success = false, message = "文件格式不支持！", Data = new object() };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 保存聊天文件

        /// <summary>
        /// 保存聊天图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveChatImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".png" || _tp.ToLower() == ".bmp" || _tp.ToLower() == ".gif")
                    {
                        var pstr = DateTime.Now.ToString("yyyyMMddHHmmss") + Create(6);
                        path = $"/Upload/ChatImage/{pstr}/{_file[0].FileName}";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    else
                    {
                        res = new { success = false, message = "图片格式不支持！", Data = new object() };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "图片不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// 保存聊天文档(支持Word(.doc,.docx)\Excel(.xls,.xlsx)\PPT(.ppt,.pptx)\Pdf(.pdf)\TXT(.txt)部分通用格式)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveChatFile()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                        if (_tp.ToLower() == ".doc" || _tp.ToLower() == ".docx" || _tp.ToLower() == ".xls" || _tp.ToLower() == ".exe" || _tp.ToLower() == ".xlsx" || _tp.ToLower() == ".pdf" || _tp.ToLower() == ".ppt" || _tp.ToLower() == ".pptx")
                        {
                            path = $"/Upload/ChatFiles/{DateTime.Now.ToString("yyyyMMdd")}/{_file[0].FileName.Replace(_tp,"") + DateTime.Now.ToString("yyyyMMddHHmmss")+ _tp }";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[0].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[0].FileName.Replace(_tp, ""),
                                data = url
                            };
                        }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// 保存聊天文档(支持Word(.doc,.docx)\Excel(.xls,.xlsx)\PPT(.ppt,.pptx)\Pdf(.pdf)\TXT(.txt)部分通用格式)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveTheDocments()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".doc" || _tp.ToLower() == ".docx" || _tp.ToLower() == ".xls" || _tp.ToLower() == ".exe" || _tp.ToLower() == ".xlsx" || _tp.ToLower() == ".pdf" || _tp.ToLower() == ".ppt" || _tp.ToLower() == ".pptx")
                    {
                        path = $"/Upload/ChatFiles/{DateTime.Now.ToString("yyyyMMdd")}/{_file[0].FileName.Replace(_tp, "") + DateTime.Now.ToString("yyyyMMddHHmmss")+ _tp }";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 保存聊天音频
        /// <summary>
        /// 保存聊天音频（支持".mp3"和".wav"格式）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveChatVoice()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".mp3" || _tp.ToLower() == ".wav" )
                    {
                        path = $"/Upload/ChatVoice/{DateTime.Now.ToString("yyyyMMdd")}/{_file[0].FileName.Replace(_tp, "") + DateTime.Now.ToString("yyyyMMddHHmmss") + _tp}";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion

        #region 保存聊天视频
        /// <summary>
        /// 保存聊天视频（支持".mp4"、".avi"、".flv"、".mkv"和".3gp"格式）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveChatVideo()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".mp4" || _tp.ToLower() == ".avi" || _tp.ToLower() == ".flv" || _tp.ToLower() == ".mkv" || _tp.ToLower() == ".3gp")
                    {
                        path = $"/Upload/ChatVideo/{DateTime.Now.ToString("yyyyMMdd")}/{_file[0].FileName.Replace(_tp, "") + DateTime.Now.ToString("yyyyMMddHHmmss") + _tp}";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// 保存聊天视频（支持".mp4"、".avi"、".flv"、".mkv"和".3gp"格式）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        public ActionResult SaveTheVideo()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  

                    //for (int i = 0; i < _file.Count; i++)
                    //{
                    if (_tp.ToLower() == ".mp4" || _tp.ToLower() == ".avi" || _tp.ToLower() == ".flv" || _tp.ToLower() == ".mkv" || _tp.ToLower() == ".3gp")
                    {
                        path = $"/Upload/ChatVideo/{DateTime.Now.ToString("yyyyMMdd")}/{_file[0].FileName.Replace(_tp, "") + DateTime.Now.ToString("yyyyMMddHHmmss") + _tp}";
                        string physicPath = GetAbsolutePath($"~{path}");
                        string dir = Path.GetDirectoryName(physicPath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                        {
                            _file[0].CopyTo(fs);
                        }

                        string url = path;
                        res = new
                        {
                            Success = true,
                            message = _file[0].FileName.Replace(_tp, ""),
                            data = url
                        };
                    }
                    //}
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
        #endregion


        #region//
        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="length">字符串的长度</param>
        /// <returns></returns>
        private string Create(int length)
        {
            // 创建一个StringBuilder对象存储密码
            StringBuilder sb = new StringBuilder();
            //使用for循环把单个字符填充进StringBuilder对象里面变成14位密码字符串
            for (int i = 0; i < length; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                //随机选择里面其中的一种字符生成
                switch (random.Next(3))
                {
                    case 0:
                        //调用生成生成随机数字的方法
                        sb.Append(createNum());
                        break;
                    case 1:
                        //调用生成生成随机小写字母的方法
                        sb.Append(createSmallAbc());
                        break;
                    case 2:
                        //调用生成生成随机大写字母的方法
                        sb.Append(createBigAbc());
                        break;
                }
            }
            return sb.ToString();
        }


        /// <summary>
        /// 生成单个随机数字
        /// </summary>
        private int createNum()
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int num = random.Next(10);
            return num;
        }

        /// <summary>
        /// 生成单个大写随机字母
        /// </summary>
        private string createBigAbc()
        {
            //A-Z的 ASCII值为65-90
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int num = random.Next(65, 91);
            string abc = Convert.ToChar(num).ToString();
            return abc;
        }

        /// <summary>
        /// 生成单个小写随机字母
        /// </summary>
        private string createSmallAbc()
        {
            //a-z的 ASCII值为97-122
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int num = random.Next(97, 123);
            string abc = Convert.ToChar(num).ToString();
            return abc;
        }

        #endregion
    }
}